/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.manager.create;

import br.com.foxline.enums.CivilStatus;
import br.com.foxline.enums.Gender;
import br.com.foxline.enums.TypeAddress;
import br.com.foxline.model.Address;
import br.com.foxline.model.Annex;
import br.com.foxline.model.City;
import br.com.foxline.model.Customer;
import br.com.foxline.service.CityService;
import br.com.foxline.service.CustomerService;
import br.com.foxline.util.Caracter;
import br.com.foxline.util.Msg;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author ely
 */
@ManagedBean
@ViewScoped
public class ManagerCreateCustomer implements Serializable {

    @EJB
    CustomerService customerService;
    @EJB
    CityService cityService;
    private Customer customer;
    private Customer conjugue;
    private Address address;
    private List<Address> addresses;
    private City city;
    private Annex annex;
    private List<Annex> annexs;
    private Address viewaddress;
    private City viewcity;
    private boolean conjugeVisible;

    public ManagerCreateCustomer() {
    }

    @PostConstruct
    public void init() {

        customer = new Customer();
        conjugue = new Customer();
        address = new Address();
        addresses = new ArrayList<Address>();
        annexs = new ArrayList<Annex>();

    }

    public List<SelectItem> getGender() {
        List<SelectItem> genders = new ArrayList<SelectItem>();
        for (Gender ms : Gender.values()) {
            genders.add(new SelectItem(ms, ms.getName()));
        }
        return genders;
    }

    public List<SelectItem> getCivilStatus() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        for (CivilStatus c : CivilStatus.values()) {
            items.add(new SelectItem(c, c.getName()));
        }
        return items;
    }

    public List<SelectItem> getTypeAddress() {
        List<SelectItem> items = new ArrayList<SelectItem>();
        for (TypeAddress c : TypeAddress.values()) {
            items.add(new SelectItem(c, c.getName()));
        }
        return items;
    }

    public void addAddress() {

        address.setCity(city);
        address.setCep(Caracter.removecaracter(address.getCep()));
        addresses.add(address);
        address = new Address();
    }

    public void edit(Address ad, int id) {
        address = ad;
        if (address.getCity() != null) {
            city = cityService.find(address.getCity().getId());
        }
        addresses.remove(id);
    }

    public void remove(int id) {
        addresses.remove(id);
    }

    public void cleanAddress() {
        this.address = new Address();
    }

    public void save() {
        try {

            if (addresses.size() > 0) {
                if (typeCStatus()) {
                    
                    customer.setSpouse(conjugue);
                    customer.setAddresses(addresses);
                    customer.setAnnexs(annexs);
                    customerService.saveCustomer(customer);
                    init();
                } else {
                    customer.setCelPhone(Caracter.removecaracter(customer.getCelPhone()));
                    customer.setFixedPhone(Caracter.removecaracter(customer.getFixedPhone()));
                    customer.setCpf(Caracter.removecaracter(customer.getCpf()));
                    customer.setAddresses(addresses);
                    customer.setAnnexs(annexs);
                    customerService.save(customer);
                    Msg.messagemInfo("Cliente Cadastrado com Sucesso");
                    init();
                }
            } else {
                Msg.messagemWarn("O Cliente deve possuir um endereço");
            }

        } catch (Exception e) {
            Msg.messagemError("Erro ao Salvar Cliente.");
        }
    }

    public boolean typeCStatus() {
        boolean b;
        if (customer.getCivilStatus() == CivilStatus.CASADO) {
            b = true;
        } else {
            b = false;
        }
        return b;
    }

    public void customerExist() {
        conjugue.setCpf(Caracter.removecaracter(conjugue.getCpf()));
        Customer c = customerService.existCustomer(conjugue);
        if (c != null) {
            conjugue = customerService.find(c.getId());
            
            customer.setSpouse(conjugue);
        } else {
             if (conjugue.getCpf() != null) {
                String cpf;
                cpf = conjugue.getCpf();
                conjugue = new Customer();
                conjugue.setCpf(cpf);
                conjugue.setCelPhone(Caracter.removecaracter(conjugue.getCelPhone()));
            }
        }
    }

    public void handle(FileUploadEvent event) throws IOException {
        handleAux(event.getFile().getContents(), event.getFile().getFileName(),
                event.getFile().getContentType(), event.getFile().getInputstream());
    }

    private void handleAux(byte[] imageBytes, String nome, String tipo, InputStream is) throws IOException {

        byte[] bytes = null;

        annex = new Annex();
        bytes = imageBytes;
        annex.setType(tipo);
        annex.setDescricao(nome);
        if (bytes != null) {
            annex.setFileByte(bytes);
            annexs.add(annex);
            annex = new Annex();
            Msg.messagemWarn("Anexado com Sucesso ");
        }

    }

    public void viewAddress(Address ad) {
        this.viewaddress = ad;
        if (this.viewaddress.getCity() != null) {
            viewcity = cityService.find(this.viewaddress.getCity().getId());
        }
    }

    public void ajaxTypeCivisStatus() {
        customer.setCivilStatus(customer.getCivilStatus());
        conjugeVisible = customer.getCivilStatus().equals(CivilStatus.CASADO);
        System.out.println(customer);
    }

    public List<City> autocompleteCity(String query) {
        return cityService.autocomplete(query);
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public List<Address> getAddresses() {
        return addresses;
    }

    public void setAddresses(List<Address> addresses) {
        this.addresses = addresses;
    }

    public Customer getConjugue() {
        return conjugue;
    }

    public void setConjugue(Customer conjugue) {
        this.conjugue = conjugue;
    }

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }

    public Address getViewaddress() {
        return viewaddress;
    }

    public void setViewaddress(Address viewaddress) {
        this.viewaddress = viewaddress;
    }

    public City getViewcity() {
        return viewcity;
    }

    public void setViewcity(City viewcity) {
        this.viewcity = viewcity;
    }

    public boolean isConjugeVisible() {
        return conjugeVisible;
    }

    public void setConjugeVisible(boolean conjugeVisible) {
        this.conjugeVisible = conjugeVisible;
    }
    
}
